#include "mainwindow.h"
#include "mytask.h"
#include <QDebug>
#include <QSerialPortInfo>


float Qt_Anchor_A_Y_top = 30;   //最上面边缘
float Qt_Anchor_G_Y_bottom = 700;  //最下边边缘
//(110,700)原点
float Qt_Anchor_G_X_Left = 30;  //最左边边缘
float Qt_Anchor_E_X_Right = 940;  //最右边边缘

float Real_Anchor_Y=(8*60+7)*2;
float Real_Anchor_X=660+83+26+27*30+18;



int TagPoint[3][1024*1024];
int track_num[3] = {0,0,0};
int track_flag = 0;  //记录标志
int track_draw = 0;  //绘制标志
int Anchor_Num=0; //存放串口接收到的基站数量
int Tag_Num=0;
int Last_Anchor_Num = 0;
int Last_Tag_Num = 0;

QString Uart_Str;

MyTask::MyTask()
{

}

void MainWindow::Uart_CallBack()
{
    static int rx_flag= 0;

    QByteArray temp = MySerial.readAll(); //好像一次最读32个字节
    Uart_Str.append(QString(temp));  //bytearray 转 string
    qDebug()<<"Str"<<Uart_Str;
//    qDebug()<<"temp"<<temp;
    int rx_len = Uart_Str.size();
    qDebug() <<rx_len;
    if(rx_len>160)
    {
        Uart_Str.clear();
    }
    if(Uart_Str[0] == '{') //设备搜索ACK
    {
        rx_flag=1;
    }
    else if(Uart_Str[0] == '[')  //被动接收
    {
        rx_flag=2;
    }
    else Uart_Str.clear();

    if(rx_flag==1 && Uart_Str[rx_len-1]=='}')
    {
//        qDebug()<<Uart_Str;
        Uart_Data_Deal(Uart_Str);
        qDebug()<<"display num = "<<Anchor_Num+Tag_Num;
        Unit_Data_Deal(Anchor_Num + Tag_Num);
        Uart_Str.clear();
        rx_flag = 0;
        timer2->start();
        qDebug()<<"recv finsh!!!!";
    }
    else  if(rx_flag==2 && Uart_Str[rx_len-1]==']')
    {
        Uart_Data_Deal__(Uart_Str);
        Unit_Data_Deal(Anchor_Num + Tag_Num);
        Uart_Str.clear();
        rx_flag = 0;
    }
    /*
     * 是否属于主动搜索（主动搜索即会把那些离线的给去掉）
     * 标签数量
     * 基站数量
     * 每一个基站的地址、状态、坐标、led、信道
     * 每一个标签的地址、状态、坐标、led、信道
     */
}

void MainWindow::Unit_Widget_Init()
{
    QStringList head;
    head<<"单元类型"<<"地址"<<"信道"<<"坐标"<<"状态"<<"闪灯";
    ui->tableWidget->setColumnCount(COLUMN);
    ui->tableWidget->verticalHeader()->hide();
    ui->tableWidget->setStyleSheet(
                                "QHeaderView::section{background:rgb(154, 192, 230);color: white;}"
                                  "QTabBar::tab { height: 25px; width:35px;color: white; padding: 0px;}"
                                 "QTabBar::tab:selected { background: lightgray; } "
                                   "QCheckBox { color: red } "
                                   "QScrollBar{background:rgb(154, 192, 230)}"
                                   );
    ui->tableWidget->horizontalHeader()->setMinimumHeight(60);
//    ui->tableWidget->horizontalHeader()->setFont()
    ui->tableWidget->setHorizontalHeaderLabels(head);
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); //自动填充
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers); //只读
}
void MainWindow::Tag_Widget_Init()
{
    QStringList head1;
    head1<<"序号"<<"地址"<<"X"<<"Y"<<"信号强度";
    ui->tableWidget_Tag->setColumnCount(5);
    ui->tableWidget_Tag->verticalHeader()->hide();
    ui->tableWidget_Tag->setStyleSheet("QHeaderView::section{background:rgb(159, 178, 225);color: white;}"
                                  "QTabBar::tab { height: 25px; width:25px;color: white; padding: 0px;}"
                                 "QTabBar::tab:selected { background: lightgray; } "
                                   "QCheckBox { color: red } "
                                   "QScrollBar{background:rgb(159, 178, 225); width: 15px;}"
                                       );
    ui->tableWidget_Tag->setHorizontalHeaderLabels(head1);
    ui->tableWidget_Tag->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); //自动填充
    ui->tableWidget_Tag->setEditTriggers(QAbstractItemView::NoEditTriggers); //只读
}

void MainWindow::Unit_Data_Deal(int Anchor_Num)
{
    static int UnitNum = 0;
    UnitNum = Anchor_Num;

//    for(int i =0 ; i<num ;i++)
//    {
//        //把arg里面的i换成实际数据
//        TempIndex[i] = QString( "%1" ).arg(i, 0, 10 );

//        //整型转16进制字符串，第一种方法
//        TempAddr[i] = QString( "0x%1" ).arg(i, 0, 16 );
//        //整型转16进制字符串，第二种方法
//        TempAddr[i] = QString::number(i,16);
//        TempAddr[i] = "0x" + TempAddr[i];
//    }

    ui->tableWidget->setRowCount(UnitNum);//根据得到的基站数量来显示多少行
    for(int i =0 ; i<UnitNum ;i++) //根据得到的基站数量来显示多少行
    {
        TempIndex[i] = QString( "%1" ).arg(i, 2, 10 , QChar('0'));
        //第二个参数是数据位数，第四个参数位数不够时补充的字符
        TempAddr[0] = QString("0x%1").arg(Unit[i].Addr, 4, 16,QChar('0'));

        QFont qfont ;
        qfont.setPointSize(13);

        QTableWidgetItem *ItemType = new QTableWidgetItem;
        ItemType->setTextAlignment(Qt::AlignCenter);
        if(Unit[i].Type == "基站")
        {
            ItemType->setTextColor(QColor(60,76,172));
        }
        else
        {
//            Unit[i].label = new QLabel(ui->tab_2);
//            Unit[i].label->setObjectName(QString("Unit[%1].label").arg(i));
//            Unit[i].label->setGeometry(QRect(300, 160, 41, 16));
//            Unit[i].label->move(Unit[i].X,Unit[i].Y);
//            Unit[i].label->setStyleSheet("border:1px solid black;border-radius:13px;min-width: 26px; min-height: 26px;max-width:26px; max-height: 26px;"
//                                         "background:rgb(200,101,102);");
//            Unit[i].label->installEventFilter(this);
//            flag=1;
            ItemType->setTextColor(QColor(0,139,139));
        }
        ItemType->setFont(qfont);
        ItemType->setText(Unit[i].Type);
        ui->tableWidget->setItem(i,0,ItemType);   //显示类别

        qfont.setPointSize(11);

        QTableWidgetItem *ItemAddr = new QTableWidgetItem;
        ItemAddr->setTextAlignment(Qt::AlignCenter);
        ItemAddr->setFont(qfont);
        ItemAddr->setText(TempAddr[0]);
        //qDebug()<<"地址："<<ItemAddr->text();
        ui->tableWidget->setItem(i,1,ItemAddr);   //显示地址

        QTableWidgetItem *ItemChannel = new QTableWidgetItem;
        ItemChannel->setTextAlignment(Qt::AlignCenter);
        ItemChannel->setFont(qfont);
        ItemChannel->setData(Qt::DisplayRole,Unit[i].Channel);
        ui->tableWidget->setItem(i,2,ItemChannel); //显示信道
        if("标签"==Unit[i].Type)
        {
            QTableWidgetItem *ItemChannel = new QTableWidgetItem;
            ItemChannel->setTextAlignment(Qt::AlignCenter);
            ItemChannel->setText("X");
            ui->tableWidget->setItem(i,2,ItemChannel);
        }

        QString Pos = '('+QString::number(Unit[i].X)+','+QString::number(Unit[i].Y)+')';
        QTableWidgetItem *ItemPos = new QTableWidgetItem;
        ItemPos->setFont(qfont);
        ItemPos->setText(Pos);
        ItemPos->setTextAlignment(Qt::AlignCenter);
        ui->tableWidget->setItem(i,3,ItemPos);     //显示坐标

        QTableWidgetItem *ItemActive = new QTableWidgetItem;
        if("离线" == Unit[i].Active)
        {
            ItemActive->setTextColor(Qt::red);
        }
        else ItemActive->setTextColor(Qt::black);
        ItemActive->setFont(qfont);
        ItemActive->setText(Unit[i].Active);
        ItemActive->setTextAlignment(Qt::AlignCenter);
        ui->tableWidget->setItem(i,4,ItemActive);  //显示状态

        checkBox[i] = new QCheckBox();
        hLayout[i] = new QHBoxLayout();
        cell_widget[i] = new QWidget();

        if(Unit[i].Led==0)checkBox[i]->setCheckState(Qt::Unchecked);
        else checkBox[i]->setCheckState(Qt::Checked);
        hLayout[i]->addWidget(checkBox[i]);
        hLayout[i]->setAlignment(cell_widget[i],Qt::AlignCenter);
        hLayout[i]->setContentsMargins(70,0,20,0);
        cell_widget[i]->setLayout(hLayout[i]);
        ui->tableWidget->setCellWidget(i,5,cell_widget[i]);

        ui->tableWidget->setRowHeight(i, 50);
    }
    ui->tableWidget->sortItems(0,Qt::DescendingOrder);//通过地址来排序
}

void MainWindow::Tag_Data_Deal(int Tag_Num)
{
    static int num = 0;
    num = Tag_Num;

    ui->tableWidget_Tag->setRowCount(num);
//    ui->tableWidget_Tag->setItem(0,1,new QTableWidgetItem("1"));
//    ui->tableWidget_Tag->setItem(1,1,new QTableWidgetItem("3"));
//    ui->tableWidget_Tag->setItem(2,1,new QTableWidgetItem("2"));
//    ui->tableWidget_Tag->setItem(3,1,new QTableWidgetItem("5"));
//    ui->tableWidget_Tag->setItem(4,1,new QTableWidgetItem("4"));
    for(int i =0 ; i<20 ;i++) //根据得到的基站数量来显示多少行
    {
        QTableWidgetItem *item = new QTableWidgetItem;
        QString s;
        s=QString("%1").arg(i,0,16);
        item->setText(s);
        ui->tableWidget_Tag->setItem(i,1,item);
    }
}


//搜索串口
void MainWindow::Search_Uart()
{
    static int temp=1;
    ui->uart_box->clear();
    foreach(*info, QSerialPortInfo::availablePorts())//第二个参数会返回一个list，第一个参数会遍历list中的每一项
    {
        qDebug() << "Name : " << info->portName();
        qDebug() << "Description : " << info->description();
        qDebug() << "Manufacturer: " << info->manufacturer();
        qDebug() << "Serial Number: " << info->serialNumber();
        qDebug() << "System Location: " << info->systemLocation();
        ui->uart_box->addItem(info->portName());
    }
    if(temp == 1)
    {
        //波特率list
        QStringList boud_list;
        boud_list<<"9600"<<"38400"<<"76800"<<"115200";
        ui->boud_box->addItems(boud_list);
        temp=0;
        connect(&MySerial, &QSerialPort::readyRead,this,&MainWindow::Uart_CallBack);
    }
}

//！！！！！！！！！这里可以改进成链表
void MainWindow::Uart_Data_Deal(QString input)
{
    current_date_time =QDateTime::currentDateTime();
    current_date = current_date_time.toString("yyyy.MM.dd hh:mm:ss");
    ui->textEdit->append("【接收】（"+current_date+"） "+input);
    QStringList step1 = input.split(';');
    Last_Anchor_Num = Anchor_Num;
    Last_Tag_Num = Tag_Num;

    for(int i = 0 ; i <= 3 ; i++)
    {
        if(i == 0)  //目前只能显示三个标签label
        {
            ui->label_tag->hide();
        }
        if(i == 1)
        {
            ui->label_tag_2->hide();
        }
        if(i == 2)
        {
            ui->label_tag_3->hide();
        }
       // Unit[Last_Anchor_Num+i].label->hide();
    }
    Anchor_Num = step1[1].toInt();
    Tag_Num = step1[3].toInt();
    int input_len = step1[0].split('{')[1].toInt();
    qDebug()<<"anchor num:"<<Anchor_Num<<",tag num:"<<Tag_Num<<"rx len:"<<input_len;

    if(Anchor_Num!=0)
    {
        int temp = 0;
        bool find = false;
        int add_num=0;
        QStringList step2 = step1[2].split('|');
        for(int i = 0 ; i < Anchor_Num ; i++)
        {
            QStringList step3 = step2[i].split(',');
            int temp_addr = step3[0].split('(')[1].toUInt();

            //判断解算出来的地址之前有没有
            for (int j = 0 ; j < Last_Anchor_Num ;j++)
            {
                if(temp_addr == Unit[j].Addr)  //代表获得的该基站地址 之前已经有了
                {
                    temp = j;
                    find = true;
                    break;
                }
                else find = false;  //代表获得的该基站地址 之前没有
            }
            if(find == true) //之前有的话，就直接更新对应的基站数组
            {
                Unit[temp].Addr = step3[0].split('(')[1].toUInt();
                Unit[temp].Type = "基站";
                Unit[temp].Channel = step3[1].toUInt();
                Unit[temp].Active = "在线";
                Unit[temp].X = step3[2].toUInt();
                Unit[temp].Y = step3[3].toUInt();
                Unit[temp].Led = step3[4].split(')')[0].toUInt();
                Unit[temp].timer = 0;
                Unit[temp].Active = "在线";
                qDebug()<<"Update Anchor:"<<QString("0x%1").arg(Unit[i].Addr, 4, 16,QChar('0'));
            }
            else //之前没有的话，就新增加进去，并且把标签后移
            {
                for(int i = 0 ; i < Last_Tag_Num ; i++)
                {
//                    Unit[Last_Anchor_Num+Last_Tag_Num+1] = Unit[Last_Anchor_Num+Last_Tag_Num];
                    Unit[Last_Anchor_Num+Last_Tag_Num-i] = Unit[Last_Anchor_Num+Last_Tag_Num-1-i];
                }
                Unit[Last_Anchor_Num + add_num].Addr = step3[0].split('(')[1].toUInt();
                Unit[Last_Anchor_Num + add_num].Type = "基站";
                Unit[Last_Anchor_Num + add_num].Channel = step3[1].toUInt();
                Unit[Last_Anchor_Num + add_num].Active = "在线";
                Unit[Last_Anchor_Num + add_num].X = step3[2].toUInt();
                Unit[Last_Anchor_Num + add_num].Y = step3[3].toUInt();
                Unit[Last_Anchor_Num + add_num].Led = step3[4].split(')')[0].toUInt();
                Unit[Last_Anchor_Num + add_num].timer = 0;
                add_num++;
                qDebug()<<"Add Anchor:"<<QString("0x%1").arg(Unit[i].Addr, 4, 16,QChar('0'));
            }
        }
        Anchor_Num = Last_Anchor_Num + add_num; //上一次的基站数量 + 这一次新增的基站数量
//        qDebug()<<"fisrt anchor num = "<<Anchor_Num;
        Select_Anchor(Anchor_Num);
    }
    if(Tag_Num != 0)  //标签不支持离线，插入即出现在网络中，拔出即消失在网络中
    {
        int temp = 0;
        bool find = false;
        int add_num=0;
        QStringList step2 = step1[4].split('|');
        for(int i = 0 ; i < Tag_Num ; i++)  //第一层遍历索引
        {
            QStringList step3 = step2[i].split(',');
            int temp_addr = step3[0].split('(')[1].toUInt();

            for (int j = 0 ; j < Last_Tag_Num+add_num ; j++)   //第二层遍历地址
            {
                if(temp_addr == Unit[Anchor_Num+j].Addr)  //代表获得的该基站地址 之前已经有了
              //  if(temp_addr == Unit[Last_Anchor_Num+j].Addr)  //代表获得的该基站地址 之前已经有了
                {
                    temp = j;
                    find = true;
                    break;
                }
                else find = false;  //代表获得的该基站地址 之前没有
            }
            if( find == true )
            {
                float temp_x = step3[2].toUInt();
                float temp_y = step3[3].toUInt();

                qDebug()<<"原始数据"<<temp_x<<","<<temp_y;
                temp_x = temp_x * (Qt_Anchor_E_X_Right-Qt_Anchor_G_X_Left) / Real_Anchor_X;
                temp_y = temp_y * (Qt_Anchor_G_Y_bottom -Qt_Anchor_A_Y_top) / Real_Anchor_Y;
                temp_y = Qt_Anchor_G_Y_bottom - temp_y;

                if(temp_x > Qt_Anchor_E_X_Right)temp_x = Qt_Anchor_E_X_Right;
                else if(temp_x<Qt_Anchor_G_X_Left)temp_x=Qt_Anchor_G_X_Left;
                if(temp_y > Qt_Anchor_G_Y_bottom)temp_y = Qt_Anchor_G_Y_bottom;
                else if(temp_y<Qt_Anchor_A_Y_top)temp_y=Qt_Anchor_A_Y_top;

                Unit[temp+Anchor_Num].Addr = step3[0].split('(')[1].toUInt();
                Unit[temp+Anchor_Num].Type = "标签";
                Unit[temp+Anchor_Num].Channel = step3[1].toUInt();
                Unit[temp+Anchor_Num].X = temp_x;
                Unit[temp+Anchor_Num].Y = temp_y;
                Unit[temp+Anchor_Num].Active = "在线";
                Unit[temp+Anchor_Num].Led = step3[4].split(')')[0].toUInt();
                Unit[temp+Anchor_Num].timer = 0;

                if(track_flag==1)
                {
                    TagPoint[i][track_num[i]] = Unit[i+Anchor_Num].X;
                    TagPoint[i][track_num[i]+1] = Unit[i+Anchor_Num].Y;
                    track_num[i]+=2;
                }
//                if(i == 0)
//                {
//                   ui->label_tag->show();
//                   ui->label_tag->move(Unit[i+Anchor_Num].X,Unit[i+Anchor_Num].Y);
//                }
//                if(i == 1)
//                {
//                    ui->label_tag_2->show();
//                    ui->label_tag_2->move(Unit[i+Anchor_Num].X,Unit[i+Anchor_Num].Y);
//                }
//                if(i == 2)
//                {
//                    ui->label_tag_3->show();
//                    ui->label_tag_3->move(Unit[i+Anchor_Num].X,Unit[i+Anchor_Num].Y);
//                }

               qDebug()<<"Update Tag:"<<QString("0x%1").arg(Unit[temp+Anchor_Num].Addr, 4, 16,QChar('0'));
            }
            else
            {
                float temp_x = step3[2].toUInt();
                float temp_y = step3[3].toUInt();
                qDebug()<<"原始数据"<<temp_x<<","<<temp_y;

                temp_x = temp_x * (Qt_Anchor_E_X_Right-Qt_Anchor_G_X_Left) / Real_Anchor_X;
                temp_y = temp_y * (Qt_Anchor_G_Y_bottom -Qt_Anchor_A_Y_top) / Real_Anchor_Y;
                temp_y = Qt_Anchor_G_Y_bottom - temp_y;

                if(temp_x > Qt_Anchor_E_X_Right)temp_x = Qt_Anchor_E_X_Right;
                else if(temp_x<Qt_Anchor_G_X_Left)temp_x=Qt_Anchor_G_X_Left;
                if(temp_y > Qt_Anchor_G_Y_bottom)temp_y = Qt_Anchor_G_Y_bottom;
                else if(temp_y<Qt_Anchor_A_Y_top)temp_y=Qt_Anchor_A_Y_top;

                Unit[Anchor_Num + Last_Tag_Num + add_num].Addr = step3[0].split('(')[1].toUInt();
                Unit[Anchor_Num + Last_Tag_Num + add_num].Type = "标签";
                Unit[Anchor_Num + Last_Tag_Num + add_num].Channel = step3[1].toUInt();
                Unit[Anchor_Num + Last_Tag_Num + add_num].Active = "在线";
                Unit[Anchor_Num + Last_Tag_Num + add_num].X = temp_x;
                Unit[Anchor_Num + Last_Tag_Num + add_num].Y = temp_y;
                Unit[Anchor_Num + Last_Tag_Num + add_num].Led = step3[4].split(')')[0].toUInt();
                Unit[Anchor_Num + Last_Tag_Num + add_num].timer = 0;
                add_num++;
                if(track_flag==1)
                {
                    TagPoint[i][track_num[i]] = Unit[i+Anchor_Num].X;
                    TagPoint[i][track_num[i]+1] = Unit[i+Anchor_Num].Y;
                    track_num[i]+=2;
                }
//                if(i == 0)
//                {
//                   ui->label_tag->show();
//                   ui->label_tag->move(Unit[i+Anchor_Num].X,Unit[i+Anchor_Num].Y);
//                }
//                if(i == 1)
//                {
//                    ui->label_tag_2->show();
//                    ui->label_tag_2->move(Unit[i+Anchor_Num].X,Unit[i+Anchor_Num].Y);
//                }
//                if(i == 2)
//                {
//                    ui->label_tag_3->show();
//                    ui->label_tag_3->move(Unit[i+Anchor_Num].X,Unit[i+Anchor_Num].Y);
//                }
                qDebug()<<"Add Tag:"<<QString("0x%1").arg(Unit[Anchor_Num + Last_Tag_Num + add_num].Addr, 4, 16,QChar('0'));
            }
        }
        Tag_Num = Last_Tag_Num + add_num; //上一次的基站数量 + 这一次新增的基站数量
        for(int z = 0 ; z < Tag_Num ; z++)
        {
            if( z == 0 )
            {
                ui->label_tag->show();
                ui->label_tag->move(Unit[z+Anchor_Num].X,Unit[z+Anchor_Num].Y);
            }
            if(z == 1)
            {
                ui->label_tag_2->show();
                ui->label_tag_2->move(Unit[z+Anchor_Num].X,Unit[z+Anchor_Num].Y);
            }
            if(z == 2)
            {
                ui->label_tag_3->show();
                ui->label_tag_3->move(Unit[z+Anchor_Num].X,Unit[z+Anchor_Num].Y);
            }
        }
    }
    qDebug()<<"anchor num ="<<Anchor_Num << " tag num ="<<Tag_Num;
}
void MainWindow::Uart_Data_Deal__(QString input)
{
    current_date = current_date_time.toString("yyyy.MM.dd hh:mm:ss");
    ui->textEdit->append("【接收】（"+current_date+"） "+input);
    bool find_flag=false;
    QStringList step1 = input.split(',');
    if(step1[0].split('[')[1] == '0')  //基站
    {
        int temp_addr = step1[1].toUInt();
        for(int i = 0 ; i < Anchor_Num ; i ++)
        {
            if(temp_addr == Unit[i].Addr)
            {
                Unit[i].X = step1[2].toUInt();
                Unit[i].Y = step1[3].toUInt();
                Unit[i].Led = step1[4].split(']')[0].toUInt();
                Unit[i].timer = 0;
                Unit[i].Active = "在线";
                find_flag = true;
                break;
            }
        }
    }
    else if(step1[0].split('[')[1] == '1') //标签
    {
        int temp_addr = step1[1].toUInt();
        //qDebug()<<temp_addr;
        for(int i = 0 ; i < Tag_Num ; i ++)
        {
            if(temp_addr == Unit[i+Anchor_Num].Addr)
            {
                Unit[i+Anchor_Num].X = step1[2].toUInt();
                Unit[i+Anchor_Num].Y = step1[3].toUInt();
                Unit[i+Anchor_Num].Led = step1[4].split(']')[0].toUInt();
                Unit[i+Anchor_Num].timer = 0;
                Unit[i+Anchor_Num].Active = "在线";
                find_flag = true;
            }
            if(i == 0)
            {
                ui->label_tag->show();
                ui->label_tag->move(Unit[i+Anchor_Num].X,Unit[i+Anchor_Num].Y);
            }
            if(i == 1)
            {
                ui->label_tag_2->show();
                ui->label_tag_2->move(Unit[i+Anchor_Num].X,Unit[i+Anchor_Num].Y);
            }
            if(i == 2)
            {
                ui->label_tag_3->show();
                ui->label_tag_3->move(Unit[i+Anchor_Num].X,Unit[i+Anchor_Num].Y);
            }
        }
    }
    if(find_flag == false)
    {
        qDebug()<<"Not find Addr : "<<QString("0x%1").arg(step1[1].toUInt(), 4, 16,QChar('0'));
    }
    else
    {
        qDebug()<<"Update data :"<<QString("0x%1").arg(step1[1].toUInt(), 4, 16,QChar('0'));
    }
}


void MainWindow::Select_Anchor(int num)
{
    for( int i = 0 ; i < num ; i++)
    {
        if(Unit[i].Addr == 4369) //0x1111
        {
//            qDebug()<< Unit[i].Addr;
          ui->label_anchor_4->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
        }
        if(Unit[i].Addr == 8738) //0x2222
        {
//            qDebug()<< Unit[i].Addr;
            ui->label_anchor_6->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
        }
        if(Unit[i].Addr == 13107) //0x3333
        {
//            qDebug()<< Unit[i].Addr;
            ui->label_anchor_8->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
        }
        if(Unit[i].Addr == 17476) //0x4444
        {
//            qDebug()<< Unit[i].Addr;
            ui->label_anchor_3->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
        }
    }

//    switch (num) {
//    case 0:
//        ui->label_anchor->setStyleSheet("border:4px solid red;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_2->setStyleSheet("border:4px solid red;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_3->setStyleSheet("border:4px solid red;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_4->setStyleSheet("border:4px solid red;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_5->setStyleSheet("border:4px solid red;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_6->setStyleSheet("border:4px solid red;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_7->setStyleSheet("border:4px solid red;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_8->setStyleSheet("border:4px solid red;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_9->setStyleSheet("border:4px solid red;border-radius:10px;padding:6px 4px;");
//        break;
//    case 1:
//        ui->label_anchor->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_2->setStyleSheet("border:4px solid red;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_3->setStyleSheet("border:4px solid red;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_4->setStyleSheet("border:4px solid red;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_5->setStyleSheet("border:4px solid red;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_6->setStyleSheet("border:4px solid red;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_7->setStyleSheet("border:4px solid red;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_8->setStyleSheet("border:4px solid red;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_9->setStyleSheet("border:4px solid red;border-radius:10px;padding:6px 4px;");
//        break;
//    case 2:
//        ui->label_anchor->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_2->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_3->setStyleSheet("border:4px solid red;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_4->setStyleSheet("border:4px solid red;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_5->setStyleSheet("border:4px solid red;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_6->setStyleSheet("border:4px solid red;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_7->setStyleSheet("border:4px solid red;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_8->setStyleSheet("border:4px solid red;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_9->setStyleSheet("border:4px solid red;border-radius:10px;padding:6px 4px;");
//       break;
//    case 3:
//        ui->label_anchor->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_2->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_3->setStyleSheet("border:4px solid red;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_4->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_5->setStyleSheet("border:4px solid red;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_6->setStyleSheet("border:4px solid red;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_7->setStyleSheet("border:4px solid red;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_8->setStyleSheet("border:4px solid red;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_9->setStyleSheet("border:4px solid red;border-radius:10px;padding:6px 4px;");
//        break;
//    case 4:
//        ui->label_anchor->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_2->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_3->setStyleSheet("border:4px solid red;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_4->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_5->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_6->setStyleSheet("border:4px solid red;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_7->setStyleSheet("border:4px solid red;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_8->setStyleSheet("border:4px solid red;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_9->setStyleSheet("border:4px solid red;border-radius:10px;padding:6px 4px;");
//        break;
//    case 5:
//        ui->label_anchor->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_2->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_3->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_4->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_5->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_6->setStyleSheet("border:4px solid red;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_7->setStyleSheet("border:4px solid red;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_8->setStyleSheet("border:4px solid red;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_9->setStyleSheet("border:4px solid red;border-radius:10px;padding:6px 4px;");
//        break;
//    case 6:
//        ui->label_anchor->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_2->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_3->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_4->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_5->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_6->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_7->setStyleSheet("border:4px solid red;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_8->setStyleSheet("border:4px solid red;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_9->setStyleSheet("border:4px solid red;border-radius:10px;padding:6px 4px;");
//        break;
//    case 7:
//        ui->label_anchor->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_2->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_3->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_4->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_5->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_6->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_7->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_8->setStyleSheet("border:4px solid red;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_9->setStyleSheet("border:4px solid red;border-radius:10px;padding:6px 4px;");
//        break;
//    case 8:
//        ui->label_anchor->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_2->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_3->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_4->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_5->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_6->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_7->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_8->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_9->setStyleSheet("border:4px solid red;border-radius:10px;padding:6px 4px;");
//        break;
//    case 9:
//        ui->label_anchor->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_2->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_3->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_4->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_5->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_6->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_7->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_8->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
//        ui->label_anchor_9->setStyleSheet("border:4px solid blue;border-radius:10px;padding:6px 4px;");
//        break;
//    }
}
